##################################################################
# File : /lua/utilities.lua
# Description : Utility functions for scripts.
# Author : GPG
##################################################################

function GetPointsAroundCircle(center, radius, segments)
    local points = {}
    local recip = 1.0 / segments
    local major = Vector(0,0,radius)
    local minor = Vector(radius,0,0)
    for i = 1, segments do
        local theta = 2*math.pi*i*recip
        local dz = VMult(major, math.cos(theta))
        local dx = VMult(minor, math.sin(theta))
        local p = VAdd(center, VAdd(dx, dz))
        table.insert(points, p)
    end
    return points
end

function GetPointsAroundArc(center, radius, segments, arcsize, startangle, offset)
    local points = {}
    local recip = arcsize / segments
    local major = Vector(0,0,radius)
    local minor = Vector(radius,0,0)
    startangle = startangle - offset;
    for i = 1, segments do
        local theta = startangle + i * recip * 2 * math.pi
        local dz = VMult(major, math.cos(theta))
        local dx = VMult(minor, math.sin(theta))
        local p = VAdd(center, VAdd(dx, dz))
        table.insert(points, p)
    end
    return points
end

function GetRandomFloat( nmin, nmax)
    return (Random() * (nmax - nmin) + nmin)
end

function GetRandomOffset( sx, sy, sz, scalar )
    sx = sx * scalar
    sy = sy * scalar
    sz = sz * scalar
    local x = Random()*sx - (sx*.5)
    local y = Random()*sy
    local z = Random()*sz - (sz*.5)
    return x,y,z
end

function UserConRequest(string)
    if not Sync.UserConRequests then
        Sync.UserConRequests = {}
    end
    table.insert(Sync.UserConRequests, string)
end

function RippleWorld()
    UserConRequest("GlobalRipple")
end

function TableCat( ... )
    local ret = {}
    for index = 1, table.getn(arg) do
        if arg[index] != nil then
            for k, v in arg[index] do
                table.insert( ret, v )
            end
        end
    end
    return ret
end